package com.service.Impl;

import com.mapper.CCommentMapper;
import com.mapper.CFishingPondMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CFishingPond;
import com.pojo.vo.CFishingPondVo;
import com.pojo.vo.CommentVo;
import com.service.CFishingPondService;
import com.service.CFishingShopService;
import com.util.LoLaToDistanceUtil;
import com.util.PojoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CFishingPondServiceImpl implements CFishingPondService {
    @Autowired
    CFishingPondMapper cFishingPondMapper;
    @Autowired
    CCommentMapper cCommentMapper;
    @Autowired
    CFishingShopService cFishingShopService;

    @Override
    public Message<List<CFishingPondVo>> list(CFishingPondVo cFishingPondVo) {
        Double latitude = null;
        Double longtitude = null;
        QueryWrapper<CFishingPond> queryWrapper = new QueryWrapper<>();
        if (cFishingPondVo != null) {
            if (!StringUtils.isEmpty(cFishingPondVo.getLatitude()) && !StringUtils.isEmpty(cFishingPondVo.getLongtitude())) {
                latitude = cFishingPondVo.getLatitude();
                longtitude = cFishingPondVo.getLongtitude();
            }
            if (!StringUtils.isEmpty(cFishingPondVo.getId())) {
                queryWrapper.eq("id", cFishingPondVo.getId());
            }
            if (!StringUtils.isEmpty(cFishingPondVo.getUserNum())) {
                queryWrapper.eq("user_num", cFishingPondVo.getUserNum());
            }
            if (!StringUtils.isEmpty(cFishingPondVo.getCity())) {
                queryWrapper.eq("city", cFishingPondVo.getCity());
            }
            if (!StringUtils.isEmpty(cFishingPondVo.getAuthenticate())) {
                queryWrapper.eq("authenticate", cFishingPondVo.getAuthenticate());
            } else {
                queryWrapper.eq("authenticate", 2);
            }
            if (!StringUtils.isEmpty(cFishingPondVo.getStar())) {
                queryWrapper.eq("star", cFishingPondVo.getStar());
            }
            if (!StringUtils.isEmpty(cFishingPondVo.getChargeState())) {
                queryWrapper.eq("charge_state", cFishingPondVo.getChargeState());
            }
            if (!StringUtils.isEmpty(cFishingPondVo.getType())) {
                queryWrapper.eq("type", cFishingPondVo.getType());
            }
            if (!StringUtils.isEmpty(cFishingPondVo.getState())) {
                queryWrapper.eq("state", cFishingPondVo.getState());
            }
        }
        List<CFishingPond> cFishingPonds = cFishingPondMapper.selectList(queryWrapper);
        List<CFishingPondVo> cFishingPondVos = new ArrayList<>();
        for (int i = 0; i < cFishingPonds.size(); i++) {
            CFishingPond cFishingPond = cFishingPonds.get(i);
            Double latitude1 = cFishingPond.getLatitude();
            Double longtitude1 = cFishingPond.getLongtitude();
            CFishingPondVo cFishingPondVo1 = PojoUtil.copyObject(cFishingPond, CFishingPondVo.class);
            if (latitude != null && longtitude != null && latitude1 != null && longtitude1 != null) {
                cFishingPondVo1.setDistance(LoLaToDistanceUtil.getDistance(latitude, latitude1, longtitude, longtitude1));
            }
            List<CommentVo> commentVoList = cFishingShopService.selectComment(cFishingPondVo1.getId(), 2L);
            cFishingPondVo1.setCommentVoList(commentVoList);
            cFishingPondVos.add(cFishingPondVo1);
        }
        return Message.successMessage(1L, cFishingPondVos);
    }

    @Override
    public Message<CFishingPondVo> selectOne(Long id) {
        QueryWrapper<CFishingPond> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        CFishingPond cFishingPond = cFishingPondMapper.selectOne(queryWrapper);
        if (cFishingPond == null) {
            CFishingPondVo cFishingPondVo = PojoUtil.copyObject(cFishingPond, CFishingPondVo.class);
            List<CommentVo> commentVoList = cFishingShopService.selectComment(cFishingPondVo.getId(), 2L);
            cFishingPondVo.setCommentVoList(commentVoList);
            return Message.successMessage(1L, cFishingPondVo);
        }
        return Message.successMessage(1L, null);
    }

    @Override
    public Message<CFishingPondVo> add(CFishingPondVo cFishingPondVo) {
        int i = cFishingPondMapper.insert(PojoUtil.copyObject(cFishingPondVo, CFishingPond.class));
        if (i == 0) return Message.errorMessage("1001", "添加失败");
        return Message.successMessage(1L, cFishingPondVo);
    }

    @Override
    public Message<CFishingPondVo> edit(CFishingPondVo cFishingPondVo) {
        int i = cFishingPondMapper.updateById(PojoUtil.copyObject(cFishingPondVo, CFishingPond.class));
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        return Message.successMessage(1L, cFishingPondVo);
    }

    @Override
    public Message<CFishingPondVo> delete(Long id) {
        int i = cFishingPondMapper.deleteById(id);
        if (i == 0) return Message.errorMessage("1001", "更新失败");
        return Message.successMessage(1L, null);
    }

}
